package com.aos.erp.schools.util;

import com.aos.erp.common.config.SpringContextUtils;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.common.utils.StringUtils;
import com.aos.erp.schools.model.gatherschool.entity.GatherSchool;
import com.aos.erp.schools.model.sys.dto.UserDto;
import com.aos.erp.schools.service.gatherschool.GatherSchoolService;
import com.aos.erp.schools.service.sys.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 用户工具类
 * @author: tanzhiyu
 * @create: 2018-11-08 16:10
 **/
public class UserUtil {
    private static UserService userService = SpringContextUtils.getBean(UserService.class);
    private static GatherSchoolService schoolService=SpringContextUtils.getBean(GatherSchoolService.class);
    /**
     * 获取用户部门组织code
     * @param id    用户id
     * @return
     * @throws Exception
     */
    public static String getUserDepCode(String id) throws Exception {
        if(StringUtils.isNullOrEmpty(id) || "null".equals(id)){
            throw new BizException("请重新登录");
        }
       UserDto userDto = userService.findById(Long.valueOf(id));
       if(userDto==null){
           throw new BizException("用户id错误");
       }
       String depCode=userDto.getDepCode();
       return depCode;
    }

    /**
     * 获取用户关联的驾校id
     * @param id    用户id
     * @return
     * @throws Exception
     */
    public static String getUserSchoolId(String id) throws Exception {
        if(StringUtils.isNullOrEmpty(id) || "null".equals(id)){
            throw new BizException("请重新登录");
        }
        UserDto userDto = userService.findById(Long.valueOf(id));
        if(userDto==null){
            throw new BizException("用户id错误");
        }

        GatherSchool gatherSchool = schoolService.queryByParams(userDto.getDepCode());
        if(gatherSchool==null){
            return null;
        }else{
            return gatherSchool.getId().toString();
        }
    }

    /**
     *
     * @param id
     * @return
     * @throws Exception
     * 得到 用户 驾校信息
     */
    public static GatherSchool getUserSchool(String id) throws Exception {
        if(StringUtils.isNullOrEmpty(id) || "null".equals(id)){
            throw new BizException("请重新登录");
        }
        UserDto userDto = userService.findById(Long.valueOf(id));
        if(userDto==null){
            throw new BizException("用户id错误");
        }

        GatherSchool gatherSchool = schoolService.queryByParams(userDto.getDepCode());
        if(gatherSchool==null){
            return null;
        }else{
            return gatherSchool;
        }
    }

    /**
     * 获取用户信息
     * @param id    用户id
     * @return
     * @throws Exception
     */
    public static UserDto getUserInfo(String id) throws Exception {
        if(StringUtils.isNullOrEmpty(id) || "null".equals(id)){
            throw new BizException("请重新登录");
        }
        UserDto userDto = userService.findById(Long.valueOf(id));
        if(userDto==null){
            throw new BizException("用户id错误");
        }
        return userDto;
    }


}
